package com.hc.appInfoActivity;


import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.hc.R;
import com.hc.activity.util.DialogFactory;
import com.hc.activityService.GetDataService;
import com.hc.util.fillData.AppInfoBean;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ExpandableListView;
import android.widget.Toast;

public class AppInfoActivity extends Activity {
	private ExpandableListView listView ;
	private Timer timer = new Timer();
	private TimerTask task;
	private Handler handler;
	private AppAdapter adapter;
	private AppInfoService service;
	private List<AppInfoBean> data;
	private TextView appInfoText;
	private Button appInfoButton;
	private Dialog dialog=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_app_info);
		
		
		appInfoText = (TextView) findViewById(R.id.appInfoText);
		appInfoButton  = (Button) findViewById(R.id.appInfoButton);
		
		appInfoButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
		
				refreshApp();
				Toast.makeText(AppInfoActivity.this.getApplicationContext(),"刷新成功" , Toast.LENGTH_SHORT).show();
			}
		});
		
		
		listView = (ExpandableListView) findViewById(R.id.expandableListView1);
		GetDataService.getGalleryData();//获取在测试的信道
		service = new AppInfoService();
		data = service.getData();
		
		if(data.size()==0){
			
			dialog = DialogFactory.creatRequestDialog(AppInfoActivity.this, "正在载入数据");
			dialog.show();
		}
		
		adapter = new AppAdapter(this,data);
		
	
		listView.setAdapter(adapter);
		listView.setDescendantFocusability(ExpandableListView.FOCUS_BLOCK_DESCENDANTS );
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if(arg2<= data.size()){
					AppInfoBean bean = data.get(arg2);
					String checkStr = bean.getWifiMac()+bean.getChain();
					if(adapter.getCheckStr()==null){
						adapter.setCheckStr(checkStr);
					}else{
						
						if(adapter.getCheckStr().equals(checkStr)){
							
							adapter.setCheckStr(null);
							
						}else{
							
							adapter.setCheckStr(checkStr);
						}
					}
					
				}
				adapter.notifyDataSetChanged();
				return true;
			}
		});

				
	
		
		upViewdata();
		refreshAppInfo();
	}
	
	public void refreshAppInfo(){
		int validNum = 0;
		int noValidNum = 0;
		
		for(AppInfoBean bean:data){
			
			if(System.currentTimeMillis()-bean.getAddTime()>20000){
				
				noValidNum++;
			}else{
				validNum++;
				
			}
			
		}
		
		if(noValidNum==0){
			
			appInfoText.setText("有效数据："+validNum );
		}else{
			
			appInfoText.setText("有效数据："+validNum+" 失效数据： "+noValidNum );
		}
		
	}
	public void refreshApp(){
		
		Iterator<AppInfoBean> iterator=  AppInfoService.appInfoBeans.iterator();
		
		while(iterator.hasNext()){
			AppInfoBean bean = iterator.next();
			if(System.currentTimeMillis()-bean.getAddTime()>20000){
				
				String checkStr = bean.getWifiMac()+bean.getChain();
				if(adapter.getCheckStr()!=null){
					if(adapter.getCheckStr().equals(checkStr)){
						
						Toast.makeText(AppInfoActivity.this.getApplicationContext(),"关注的数据失效 ，已被清理" , Toast.LENGTH_SHORT).show();
					}
				}
				iterator.remove();
			}
		}
		
	}
	
	private void upViewdata() {
	
		
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				data = service.getData();
				
				if(data.size()>0&&dialog!=null){
					dialog.cancel();
					
				}
				
				adapter.setData(data);
				adapter.notifyDataSetChanged();
				refreshAppInfo();
				super.handleMessage(msg);
			}
		};

		task = new TimerTask() {
			@Override
			public void run() {
				Message message = new Message();
				message.what = 1;
				handler.sendMessage(message);
			}
		};

		timer.schedule(task, 0, 1000);

	}
	@Override
	public void onDestroy() {
		// 当结束程序时关掉Timer
		timer.cancel();

		super.onDestroy();
	}
}
